#include "bubbleSort.h"

// ------------------- CONSTRUCTORS AND DESTRUCTORS ----------------------
BubbleSort::BubbleSort(){

}

BubbleSort::~BubbleSort(){

}

//-------------------- PUBLIC METHODS ------------------------------------

// Sort items with BubbleSort algorithm
void BubbleSort::sort(Array * a){
    sort(a->getArray(), 0, a->getCnt()-1);
}

// Sort items with BubbleSort algorithm
// from - starting index, to - ending index, desc - if true, use descending order
void BubbleSort::sort(int * items, unsigned long from, unsigned long to, bool desc /*= false*/){
    sort(items, from, to, 1, desc);

//        for(int jj=from+1;jj<to-(ii-from);jj++){
}

// Sort items with BubbleSort algorithm (jump over specified fields number)
// from - starting index, to - ending index, jump - , desc - if true, use descending order
void BubbleSort::sort(int * items, unsigned long from, unsigned long to, unsigned long jump, bool desc /*= false*/){
    to++;

    if(from < 0){
        from += jump;
    }

//    cout << from << ", " << to << endl;
    for(unsigned long ii=from;ii<to;ii+=jump){
        for(unsigned long jj=from+jump;jj<to-(ii-from);jj++){
            if((items[jj-jump]>items[jj]) != desc){ //swap
                int temp=items[jj-jump];

                items[jj-jump]=items[jj];
                items[jj]=temp;
            }
        }
    }
}
